<?php
class Filem_ctl extends Ctl{
    function __construct(){
    }
    function main(array $getVars) {
        $this::sessver();
        $type=!empty($_POST['type'])?$_POST['type']:false;
        if(!$type){
            $result=xdb::getInstance()->getarray('select * from filem where uid='.XXF_UID);
            $this::show('filem',compact('result'));
        }elseif ($type=="upload"){
        	$targetPath = XXF_ROOT.'html\\uploader\\filem'; // 指定的保存目录
			//$targetPath = 'E:';
            if (!empty($_FILES)) {
                $tempFile = $_FILES['file']['tmp_name'];
                $fileSize=$_POST['fileSize'];
                $filetype=substr(strrchr($_FILES['file']['name'], '.'), 1);
                if($fileSize>20*1024*1024&&in_array($filetype,explode(",", gconf("web", "allowFile")))){
                    echo "文件太大或类型不符！";
                    exit();
                }
                $filename=XXF_UID.time().".$filetype";//生成文件名
                $array=array();
                $array2=array();
                $array['uid']=XXF_UID;
                $array['fname']=$filename;
                $array['fsize']=$fileSize;
                $array['ftitle']=$_POST['ftitle'];
                $array['ftype']=$_POST['ftype'];
                $array2['uid']=XXF_UID;
                $array2['ftitle']=$_POST['ftitle'];
                $targetFile = rtrim($targetPath,'/') . '\\' .$filename;
				move_uploaded_file($tempFile,$targetFile);//保存文件到指定目录
				if(xdb::getInstance()->getvar('select SUM(fsize) from filem where uid='.XXF_UID)>1024*1024*1024){
				    echo "存储空间不足！";exit();
				}
				xdb::getInstance()->indb("filem", $array, $array2);
				echo 1;
            }
        }elseif($type=='del'){
            $id=$_POST['id'];
            $fname=$_POST['fname'];
            $furl='uploader/filem/'.$fname;
            $array=array();
            $array['id']=$id;
            xdb::getInstance()->deldb("filem", $array);
            if(unlink($furl)){
                echo 1;
            }else{
                echo 0;
            }
        }
    }
}